#!/usr/bin/perl

##############################################################
#
#Purpose: To start periodic monitoring of the i3 servers.
#
# Periodic monitoring is done by starting a cron job at the various planetlab nodes.
# This script does the following.
#  0.  Adds a line to /etc/rc.local so that crond is started even on a reboot
#  1.  Copies the crontab file (present in this directory) to /etc/crontab in all 
#       the specified PlanetLab nodes.
#      (This crontab controls how often i3_server_check.pl should be invoked at the PlanetLab node.)
#  2.  Copies the i3_server_check.pl script to the ucb_i3 home directory on all the PlanetLab nodes.
#      (This script checks if the i3_server is running.  If not, it restarts the i3 server.
#       Please note that this script runs locally on the PlanetLab node, i.e. not from a central location.)
#  3.  Restarts the crond daemon on all the planetlab nodes.
#
#Usage: start_i3_server_monitor.pl
#		-h <host_file, i.e. list of planetlab nodes>
#
##############################################################

use strict 'vars';
package main;

use Getopt::Std;

use vars qw($opt_h);

require "read_deployment.pl";
my %deployment_details;
%deployment_details = &get_deployment_details;
my $deployment_dir = $deployment_details{"deployment_dir"};
my $deployment_username = $deployment_details{"deployment_username"};

print ("The deployment directory on the remote server is : $deployment_dir.\n");
print ("Deployment username = $deployment_username.\n");

# generate the crontab file which will point to the check script in the deployment_dir.
&generate_crontab;

# NB: The i3server_check.pl for each node was uploaded when the node was started,
# along with the start and kill scripts.

#get command line options
getopts('h:');
if ($opt_h eq "" ) {
    die "Usage: start_i3_server_monitor.pl -h <host_file>\n";
}

# Copy the crontab file to the set of planetlab nodes
system("./pl_put.pl -h $opt_h -f filelist_start_i3_server_monitor.txt -p $deployment_dir");
system("./pl_exec.pl -h $opt_h -f cmdlist_start_i3_server_monitor.txt -p $deployment_dir");
#sleep 120;

# generate the crontab file which will point to the check script in the deployment_dir.
sub generate_crontab {

	open (CRONTABFILE, ">crontab") || die "Unable to open crontab for writing.\n";
	print CRONTABFILE "SHELL=/bin/bash\n";
	print CRONTABFILE "PATH=/sbin:/bin:/usr/sbin:/usr/bin:$deployment_dir\n";
	print CRONTABFILE "MAILTO=\"\"\n";
	print CRONTABFILE "HOME=/\n";
	
	print CRONTABFILE "\n# run-parts\n\n";


	print CRONTABFILE "01 * * * * root run-parts /etc/cron.hourly\n";
	print CRONTABFILE "02 4 * * * root run-parts /etc/cron.daily\n";
	print CRONTABFILE "22 4 * * 0 root run-parts /etc/cron.weekly\n";
	print CRONTABFILE "42 4 1 * * root run-parts /etc/cron.monthly\n";

	print CRONTABFILE "\n\n";

	print CRONTABFILE "# You have to modify this part to run the i3 server active check\n";
	print CRONTABFILE "# or whatever other program you want to run periodically.\n";
	print CRONTABFILE "# This crontab file is copied as such into /etc/crontab on\n";
	print CRONTABFILE "# the planet lab node.  The contents of the previous /etc/crontab\n";
	print CRONTABFILE "# on the planetlab node will be lost.\n";

	print CRONTABFILE "# Right now, we check if the i3 server is active every five minutes.\n";
	print CRONTABFILE "# The check is performed by the i3_server_check.pl script.  If the\n";
	print CRONTABFILE "# i3 server is found to be down, the scripts restarts it.\n";

	print CRONTABFILE "\n\n";
	my $tmpMin;
	for  ($tmpMin = 0; $tmpMin <= 55; $tmpMin += 5) {
		print CRONTABFILE "$tmpMin * * * * $deployment_username $deployment_dir/i3server_check.pl\n";
	}
	close (CRONTABFILE);
}


